package com.netistate.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.netistate.base.BaseAction;
import com.netistate.pojo.Labor;
import com.netistate.pojo.LaborStat;
import com.netistate.pojo.Project;
import com.netistate.pojo.User;
import com.netistate.service.LaborService;
import com.netistate.service.ProjectService;
import com.netistate.util.GetNowTime;

public class LaborAction extends BaseAction{

	private static final long serialVersionUID = 1L;
	
	private ProjectService projectService;
	private LaborService laborService;
	
	private String id;
	private String hours;
	private String datetime;
	private String descr;
	private String createtime;
	private String projectProcess;
	private String section;
	private String isLocale;
	
	private String lid;
	private String pid;
	private String locales;
	
	//对条件
	private String projectName;
	private String laborUser;
	private String state;
	private String beginTime;
	private String endTime;
	
	private String projectNameBySection;
	private String sectionHours;
	private String sectionPerson;
	
	private String projectNameByProcess;
	private String processHours;
	private String processPerson;
	
	
	/**
	 * 根据内容去查找工时统计的东西..................有问题
	 */
	@Action(value="query_AllLaborStatBySection",results={@Result(name="success",location="/jsp/labor/laborStat.jsp")})
	@Override
	public String execute() throws Exception {
		
		@SuppressWarnings("unchecked")
		List<Project> pList = (List<Project>) getSession().get("projectList");
		
		System.out.println("projectNameBySection:"+projectNameBySection);
		System.out.println("sectionHours:"+sectionHours);
		System.out.println("sectionPerson:"+sectionPerson);
		System.out.println("section:"+section);
		
		List<LaborStat> lsList = laborService.findAllLaborStatBySection(pList,projectNameBySection,section,sectionHours,sectionPerson);
		
		getRequest().setAttribute("twoList", lsList);
		
		return SUCCESS;
	}
	
	/**
	 * 根据阶段去查找工时统计的东西
	 * @return
	 * @throws Exception
	 */
	@Action("query_AllLaborStatByProcess")
	public String query_AllLaborStatByProcess() throws Exception{
		
		System.out.println("projectNameByProcess:"+projectNameByProcess);
		System.out.println("ProcessHours:"+processHours);
		System.out.println("ProcessPerson:"+processPerson);
		System.out.println("Process:"+projectProcess);
		
		return null;
	}
	
	
	
	/**
	 * 添加工时
	 */
	@Override
	@Action("add_labor")
	public String save() throws Exception {
	
		int pid = Integer.parseInt(id);
		Project project = projectService.findProjectById(pid);
		float h = Float.parseFloat(hours);
		User u = (User) getSession().get("session_user");
		int lo = Integer.parseInt(isLocale);
		String createtime = new GetNowTime().getNowTime();
		
		Labor l = new Labor();
		l.setCreatetime(createtime);
		l.setDatetime(datetime);
		l.setDescr(descr);
		l.setHours(h);
		l.setProjectProcess(projectProcess);
		l.setSection(section);
		l.setProject(project);
		l.setUser(u);
		l.setLocale(lo);
		
		laborService.save(l);
		
		getSession().put("selectedProject", project);
		getSession().put("selectedProjectProcess", projectProcess);
		getSession().put("selectedSection", section);
		
		return null;
	}

	/**
	 * 删除工时
	 */
	@Override
	@Action("delete_labor")
	public String del() throws Exception {

		laborService.del(Integer.parseInt(id));
		return null;
	}

	/**
	 * 点击top上的工时管理，查找全部工时
	 */
	@Action("findAllLabor")
	public String findAll() throws Exception {
		
		//查找所有的工时，默认查找的是两周内的工时,以工时日期查询
		List<Labor> laborList = laborService.findAllLabor();
		//查找所有的项目
		List<Project> projectList = projectService.findAll();
		
		getSession().put("allProjectListInLabor", projectList);
		getSession().put("laborList", laborList);
		return null;
	}
	
	/**
	 * 根据条件去查询工时
	 * @return
	 * @throws Exception
	 */
	@Action(value="query_labor_by_condition",results={@Result(name="success",location="/jsp/labor/labor.jsp")})
	public String query_labor_by_condition() throws Exception{
		
		List<Labor> lList = laborService.findLaborByCondition(projectName,laborUser,
												state,beginTime,endTime,section,isLocale);
		
		getRequest().setAttribute("lList", lList);
		
		getRequest().setAttribute("queryProjectNameId", projectName);
		if(!(projectName == null || "所有".equals(projectName))){
			Project p = projectService.findProjectById(Integer.parseInt(projectName));
			getRequest().setAttribute("queryLaborPidProject", p);
		}
		getRequest().setAttribute("queryLaborUser", laborUser);
		if(!(laborUser == null || "所有".equals(laborUser))){
			User user = laborService.findUserByUid(Integer.parseInt(laborUser));
			getRequest().setAttribute("queryLaborPidUser",user);
		}
		getRequest().setAttribute("queryLaborState", state);
		getRequest().setAttribute("queryLaborSection", section);
		getRequest().setAttribute("queryIsLocale", isLocale);
		
		getRequest().setAttribute("queryLaborbeginTime", beginTime);
		getRequest().setAttribute("queryLaborEndTime", endTime);
		
		
		
		return SUCCESS;
	}

	/**
	 * 切换工时统计，然后展现出来工时统计的内容
	 */
	@Action("look_labor_stat")
	public String findById() throws Exception {
		
		@SuppressWarnings("unchecked")
		List<Project> pList = (List<Project>) getSession().get("projectList");
		
		List<LaborStat> lsList = laborService.findAllLaborStat(pList);
		getSession().put("laborStatList", lsList);
		return null;
	}

	/**
	 * 更新工时
	 */
	@Action("edit_labor")
	public String update() throws Exception {
		
		
		System.out.println("lid:"+lid);
		System.out.println("pid:"+pid);
		System.out.println("locales:"+locales);
		
		Labor l = laborService.findLaborByLId(Integer.parseInt(lid));
		Project p = projectService.findProjectById(Integer.parseInt(pid));
		
		l.setDatetime(datetime);
		l.setDescr(descr);
		l.setHours(Float.parseFloat(hours));
		l.setLocale(Integer.parseInt(locales));
		l.setProjectProcess(projectProcess);
		l.setSection(section);
		l.setProject(p);
		
		laborService.save(l);
		return null;
	}

	/**
	 * 修改工时
	 */
	@Action(value="editLabor",results={@Result(name="success",type="dispatcher",location="/jsp/labor/editLabor.jsp")})
	public String edit() throws Exception {
		
		System.out.println("id---------:"+id);
		Labor l = laborService.findLaborByLId(Integer.parseInt(id));
		
		getSession().put("laobr", l);
		return SUCCESS;
	}

	
	//get set
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getHours() {
		return hours;
	}
	public void setHours(String hours) {
		this.hours = hours;
	}
	public String getDatetime() {
		return datetime;
	}
	public void setDatetime(String datetime) {
		this.datetime = datetime;
	}
	public String getDescr() {
		return descr;
	}
	public void setDescr(String descr) {
		this.descr = descr;
	}
	public String getCreatetime() {
		return createtime;
	}
	public void setCreatetime(String createtime) {
		this.createtime = createtime;
	}
	public String getSection() {
		return section;
	}
	public void setSection(String section) {
		this.section = section;
	}
	public String getProjectProcess() {
		return projectProcess;
	}
	public void setProjectProcess(String projectProcess) {
		this.projectProcess = projectProcess;
	}
	public String getIsLocale() {
		return isLocale;
	}
	public void setIsLocale(String isLocale) {
		this.isLocale = isLocale;
	}
	public String getProjectName() {
		return projectName;
	}
	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}
	public String getLaborUser() {
		return laborUser;
	}
	public void setLaborUser(String laborUser) {
		this.laborUser = laborUser;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getBeginTime() {
		return beginTime;
	}
	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}
	public String getEndTime() {
		return endTime;
	}
	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}
	public String getLid() {
		return lid;
	}
	public void setLid(String lid) {
		this.lid = lid;
	}
	public String getPid() {
		return pid;
	}
	public void setPid(String pid) {
		this.pid = pid;
	}
	public String getLocales() {
		return locales;
	}
	public void setLocales(String locales) {
		this.locales = locales;
	}
	public String getProjectNameBySection() {
		return projectNameBySection;
	}
	public void setProjectNameBySection(String projectNameBySection) {
		this.projectNameBySection = projectNameBySection;
	}
	public String getSectionHours() {
		return sectionHours;
	}
	public void setSectionHours(String sectionHours) {
		this.sectionHours = sectionHours;
	}
	public String getSectionPerson() {
		return sectionPerson;
	}
	public void setSectionPerson(String sectionPerson) {
		this.sectionPerson = sectionPerson;
	}
	public String getProjectNameByProcess() {
		return projectNameByProcess;
	}
	public void setProjectNameByProcess(String projectNameByProcess) {
		this.projectNameByProcess = projectNameByProcess;
	}
	public String getProcessHours() {
		return processHours;
	}
	public void setProcessHours(String processHours) {
		this.processHours = processHours;
	}
	public String getProcessPerson() {
		return processPerson;
	}
	public void setProcessPerson(String processPerson) {
		this.processPerson = processPerson;
	}
	//-------
	@Autowired
	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}
	@Autowired
	public void setLaborService(LaborService laborService) {
		this.laborService = laborService;
	}
	

}
